﻿using DDBS_Server.Models;
using Raven.Client.Indexes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DDBS_Server.Models
{
    public class SamplesByTopPeaks : AbstractIndexCreationTask<Sample, SamplesByTopPeaks.Result>
    {
	    public class Result
	    {
            public double P1 { get; set; }
            public double P2 { get; set; }
            public double P3 { get; set; }
            public float R1 { get; set; }
            public float R2 { get; set; }
        }

        public SamplesByTopPeaks()
	    {
            Map = samples => from sample in samples
                             let toppeaks = sample.Peaks.OrderByDescending(p => p.V).Take(3).ToList()
                             select new
                             {
                                P1 = toppeaks[0].M,
                                P2 = toppeaks[1].M,
                                P3 = toppeaks[2].M,
                                R1 = toppeaks[0].V / toppeaks[1].V,
                                R2 = toppeaks[1].V / toppeaks[2].V,
                             };
	    }
    }

    public class Map_NameCompanyShard : AbstractIndexCreationTask<Sample, Map_NameCompanyShard.Result>
    {
	    public class Result
        {
            public string Name { get; set; }
            public string Company { get; set; }
            public string Shard { get; set; }
            public int Count { get; set; }
        }

        public Map_NameCompanyShard()
        {
            Map = samples => from sample in samples
                             select new
                             {
                                sample.Name,
                                sample.Company,
                                sample.Shard,
                                Count = 1
                             };

            Reduce = results => from result in results
                                group result by new { result.Name, result.Company, result.Shard } into g
                                select new
                                {
                                    Name = g.Key.Name,
                                    Company = g.Key.Company,
                                    Shard = g.Key.Shard,
                                    Count = g.Sum(x => x.Count)
                                };

        }

    }
}